package classfile

import (
	"encoding/binary"
)

type ClassReader struct {
	data []byte
}

/**
   ClassFile{
		u4 magic;
		u2 minor_version;
		u2 major_version;
		u2 constant_pool_count;
		cp_info  contant_pool[constant_pool_count-1];
		u2		 access_info;
		u2		this_class;
		u2		super_class;
		u2 		interface_count;
		u2		interfaces[interface_count]
		u2 		fields_count;
		field_info		fields[fields_count];
		u2 		methods_count;
		method_info methods[methods_count];
		u2 	attribute_count;
		attribute_info attributes[attribute_count];
	}

**/

func (self *ClassReader) readUint8() uint8 { // u1
	val := self.data[0]
	self.data = self.data[1:]
	return val
}
func (self *ClassReader) readUint16() uint16 {
	val := binary.BigEndian.Uint16(self.data)
	self.data = self.data[2:]
	return val
}
func (self *ClassReader) readUint32() uint32 {
	val := binary.BigEndian.Uint32(self.data)
	self.data = self.data[4:]
	return val
}
func (self *ClassReader) readUint64() uint64 {
	val := binary.BigEndian.Uint64(self.data)
	self.data = self.data[8:]
	return val
}
func (self *ClassReader) readUint16s() []uint16 {
	n := self.readUint16()
	s := make([]uint16, n)
	for i := range s {
		s[i] = self.readUint16()
	}
	return s
}
func (self *ClassReader) readBytes(n uint32) []byte {
	bytes := self.data[:n]
	self.data = self.data[n:]
	return bytes
}
